<?php

namespace Index\Controller;

use Think\Controller;

class AccountController extends Controller {

    private $dbhelp;

    function __construct() {
        parent::__construct();
        $this->dbhelp = new \Common\DAL\DB();
    }

    public function login_action() {
        $where['nick|mobile|email'] = I('account');
        $where['password'] = md5(I('password'));
        $type = I('type');
        if ($type == 1) {
            $u = M('user')->where($where)->find();
            if ($u) {
                $array['id'] = $u['id'];
                $array['nick'] = $u['nick'];
                $array['name'] = $u['name'];
                $array['type'] = 1;
                cookie('account', $array);
                $return['flag'] = true;
                $url = cookie('ref_url');
                if ($url) {
                    $return['url'] = U($url);
                } else {
                    $return['url'] = U('user/index/index');
                }
            } else {
                $return['flag'] = false;
                $return['error'] = "帐号与密码不匹配";
            }
        }
        if ($type == 2) {
            $e = M('enterprise')->where($where)->find();
            if ($e) {
                $array['id'] = $e['id'];
                $array['nick'] = $e['nick'];
                $array['name'] = $u['name'];
                $array['type'] = 2;
                cookie('account', $array);
                $return['flag'] = true;
                $return['url'] = U('enterprise/index/index');
            } else {
                $return['flag'] = false;
                $return['error'] = "帐号与密码不匹配";
            }
        }
        $this->ajaxReturn($return, 'json');
    }

    public function logout() {
        cookie('account', null);
        cookie('ref_url', null);
        header('location:' . U('index/account/login'));
        exit();
    }

    public function register() {
        $this->display(); //调用模板
    }

    //个人zhuce
    public function reg_user() {
        $data['mobile'] = I('mobile');
        $data['password'] = md5(I('password'));
        $data['account'] = 'CN' . strtotime(date('Y-m-d H:i:s'));
        $count = $this->dbhelp->count('user', array('mobile' => $data['mobile']));
        $model = new \Think\Model\AdvModel();
        $model->startTrans();
        if ($count == 0) {
            $commit = $this->dbhelp->insert('user', $data);
            if ($commit['flag']) {
                $model->commit();
                $result['flag'] = true;
                $result['msg'] = "注册成功";
                //$result['redirect'] = url('');
            } else {
                $model->rollback();
                $result['flag'] = false;
                $result['error'] = M()->getError();
            }
        } else {
            $model->rollback();
            $result['flag'] = false;
            $result['error'] = "手机号码已被注册";
        }
        $this->ajaxReturn($result, 'json');
    }

    //企业用户注册
    public function reg_enterprise() {
        $data['nick'] = trim(I('nick'));
        $data['password'] = md5(I('password'));
        $data['mobile'] = I('telephone');
        $data['email'] = I('email');
        $data['name'] = trim(I('name'));
        $data['abbreviation'] = trim(I('abbreviation'));
        $data['introduction'] = trim(I('introduction'));
        $data['city'] = I('city');
        $data['contacts'] = I('contacts');
        $count = $this->dbhelp->count('enterprise', array('name' => $data['name']));
        $model = new \Think\Model\AdvModel();
        $model->startTrans();
        if ($count == 0) {
            $commit = $this->dbhelp->insert('enterprise', $data);
            if ($commit['flag']) {
                $model->commit();
                $result['flag'] = true;
                $result['msg'] = "注册成功";
                //$result['redirect'] = url('');
            } else {
                $model->rollback();
                $result['flag'] = false;
                $result['error'] = M()->getError();
            }
        } else {
            $model->rollback();
            $result['flag'] = false;
            $result['error'] = "该企业已被注册";
        }
        $this->ajaxReturn($result, 'json');
    }

//生成验证码
    Public function verify() {
        $config = array(
            'fontSize' => 25, // 验证码字体大小
            'length' => 4, // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry(); //同一个页面多个验证码需要加ID
    }

// 检测输入的验证码是否正确，$code为用户输入的验证码字符串
    function check_verify() {
        $code = $_POST['code'];
        $id = '';
        $verify = new \Think\Verify();
        $data = $verify->check($code, $id);
        exit(json_encode($data));
    }

}
